//
//  DUPlayerController.m
//  APP
//
//  Created by duyujin1 on 2021/9/7.
//

#import "DUPlayerController.h"

@interface DUPlayerController ()
@property(nonatomic,strong) AVPlayerViewController * playerController;
@property(nonatomic,strong) AVPlayer * player;
@property(nonatomic,strong) AVAudioSession * session;
@end

@implementation DUPlayerController

- (void)viewDidLoad {
    [super viewDidLoad];
    _session = [AVAudioSession sharedInstance];
    [_session setCategory:AVAudioSessionCategoryPlayback error:nil];
    _player = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"]];
    _playerController = [[AVPlayerViewController alloc] init];
    _playerController.player = _player;
    _playerController.videoGravity = AVLayerVideoGravityResizeAspect;
    _playerController.allowsPictureInPicturePlayback = true;    //画中画，iPad可用
    _playerController.showsPlaybackControls = true;

    [self addChildViewController:_playerController];
    _playerController.view.translatesAutoresizingMaskIntoConstraints = true;    //AVPlayerViewController 内部可能是用约束写的，这句可以禁用自动约束，消除报错
    _playerController.view.frame = self.view.bounds;
    [self.view addSubview:_playerController.view];
    [_playerController.player play];    //自动播放
    [_player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        NSLog(@"播放进度：%@", @(CMTimeGetSeconds(time)));
    }];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_handlePlayEnd) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}

- (void)_handlePlayEnd {
    //播放完成后的处理
    
}


@end
